
/**
 * 发起的是 HTTPS Get请求
 * @pram url: 请求地址,协议必须为https
 * @param success 请求成功回调
 * @param fail 请求失败回调
 * @param complete 请求完成（成功或者失败）回调
 */
function rtGet(url, success, fail, complete) {
  var _url = url, _success = success, _fail = fail, _complete = complete;
  wx.request({
    url: url, method: "GET", dataType: "json",
    header: {
      'content-type': 'application/x-www-form-urlencoded' //,
      //'WX-SESSION-ID': wx.getStorageSync(constant['WX-SESSION-ID']) //每次请求带上登录标志
    },
    success: function (res) {
      if (typeof _success == "function") { _success(res.data); }
    },fail: function (res) {
      if (typeof _fail == "function") { _fail(res); }
    },complete: function (res) {
      if (typeof _complete == "function") { _complete(res); }
    }
  });
}

/**
 * 发起的是 HTTPS Post请求
 * @pram url: 请求地址,协议必须为https
 * @pram data 请求参数请求参数
 * @param success 请求成功回调
 * @param fail 请求失败回调
 * @param complete 请求完成（成功或者失败）回调
 */
function rtPost(url, data, success, fail, complete) {
  var _url = url,_data = data,_success = success,_fail = fail,_complete = complete;
  wx.request({
    url: url,data: data,method: "POST",dataType: "json",
    header: {
      'content-type': 'application/x-www-form-urlencoded'
      //'WX-SESSION-ID': wx.getStorageSync(constant['WX-SESSION-ID']) //每次请求带上登录标志
    },
    success: function (res) {
      if (res.statusCode == 404 || res.statusCode == 500) {
        wx.showToast({ title: '异常重试中', icon: 'loading', duration: 2000 });
      }
      if (typeof _success == "function") { _success(res.data); }
    },fail: function (res) {
      if (typeof _fail == "function") { _fail(res); }
    },complete: function (res) {
      if (typeof _complete == "function") { _complete(res); }
    }
  });
}



//登录鉴权,本地缓存登录状态(令牌code、用户信息、鉴权域)
function initLogin(globa, cb, complete) {
  wx.login({
    success: function (event) {
      var code = event.code;
      wx.getUserInfo({
        success: function (res) {
            //同步更新本地登录状态数据，并缓存
            wx.setStorageSync('user', res);
            //请求后台鉴权，获取业务系统的会话信息
            var datas = {
              "code":"MS01",
              "token":"",
              "datas": {
                "appId": globa.wxappId,
                "code": code,
                "crypt": encodeURIComponent(res.encryptedData),
                "iv": encodeURIComponent(res.iv)
              }
            }
            rtPost(globa.apiUrl,datas, function (res) {
                if (res.status == 404 || res.status == 500){
                  wx.showToast({ title: '异常重试中', icon: 'loading', duration: 2000 });
                }
                if (res.state == 'Y'){
                  wx.setStorageSync('session', JSON.parse(res.datas));
                  wx.setStorageSync('info', JSON.parse(res.others));
                }
                if (typeof cb == "function") { cb(); }
              },function (res) { //fail
                wx.showToast({ title: '异常重试中', icon: 'loading', duration: 2000 });
              },function (res) { //complete
                if (typeof complete == "function") { complete(); }
              }
            );             
        },fail: function (res) {
            wx.showToast({ title: '用户拒绝授权', icon: 'loading', duration: 2000 });
        }
      })
    },fail: function (res) {
      wx.showToast({ title: '微信登陆失败！', icon: 'loading', duration: 2000 });
    }
  })
}


//对外公开函数
module.exports = {
  initLogin: initLogin,
  rtGet: rtGet,
  rtPost: rtPost
}  
